<?php
/**
 * Created by PhpStorm.
 * User: woody
 * Date: 17-2-16
 * Time: 下午10:46
 */

namespace App\Http\Controllers\Api;
use App\Models\Addressapi;
use Illuminate\Http\Request;

class AddressController extends ApiController
{

    public function __construct()
    {
        parent::__construct();
        $this->_response['_active']['_model'] = 'address';
        $this->_response['_active']['_action'] = '';
        $this->_response['_title'] = '小一农货-地址管理';
    }


    public function index(){
        $data = file_get_contents('https://restapi.amap.com/v3/config/district?keywords=&subdistrict=3&key=c8149a3da34eaa5a1de69a34e1a42412');
        return $data;
    }

    /**
     * Created by PhpStorm.
     * User: 地址插入数据库
     * Date: 17-2-16
     * Time: 下午10:46
     */
    public function doAdd()
    {
        $n=0;
        $addressmodel = new Addressapi();
        $data = file_get_contents('https://restapi.amap.com/v3/config/district?keywords=&subdistrict=3&key=c8149a3da34eaa5a1de69a34e1a42412');
        $data = json_decode($data, true);
        $area_arr = $data['districts'];
        foreach ($area_arr as $b) {
            foreach ($b['districts'] as $c) {
                foreach ($c['districts'] as $d) {
                    foreach ($d['districts'] as $e) {

                        if (is_array($e['citycode'])) {
                            $e['citycode'] = "";
                        }
                        $saveData = [
                            'adcode' => $e['adcode'],
                            'name' => $e['name'],
                            'center' => $e['center'],
                            'level' => $e['level'],
                            'city_code' => $e['citycode'],
                        ];

                      //  print_r($saveData);

                        $n=$n+1;
                        if($n>500){
                            $addressmodel->saveData($saveData);
                        }
                        echo $n;
                    }
//                  if(is_array($d['citycode'])){
//                      $d['citycode']="";
//                  }
//                  $saveData = [
//                  'adcode'=>$d['adcode'],
//                  'name' => $d['name'],
//                  'center'=>$d['center'],
//                  'level' => $d['level'],
//                  'city_code' =>$d['citycode'],
//              ];
//                  $addressmodel->saveData($saveData);
//                  echo "d";
//              }

                }
            }

            //return $area_arr;


        }


    }


}